var SM = SM || {}
SM.ControlPanel = function(){
    var addDomainPanel, btnAddDomain, btnAdd, btnCancel, btnViewReport, displayReportCallbacks=[];
    var sUrl = "/index.php/rs/addDomain";
    var sReport = "/index.php/rs/viewReport";
    var sEditMessage = "/index.php/rs/editMessage";
    var failure = "failure: data not added";
    var INVALID_DATA = "invalid data";
    var SUCCESS = "success";

    var createPanel = function(){
        addDomainPanel = new YAHOO.widget.Panel("panelAddDomain", {
            width: "400px",
            fixedcenter: true,
            constraintoviewport: true,
            close: true,
            visible: false,
            draggable: true,
            modal: true,
            underlay: "matte"
        });

        addDomainPanel.setHeader("Add Domain");
        addDomainPanel.setFooter("close to cancel");
        addDomainPanel.render();
    }

    var init = function(){
        btnAddDomain = YAHOO.util.Dom.get("btnAddDomain");
        btnAdd = YAHOO.util.Dom.get("btnAdd");
        btnCancel = YAHOO.util.Dom.get("btnCancel");
        btnViewReport = YAHOO.util.Dom.get("btnViewReport");
        
        bindEvents();
        createPanel();
    }

    var displayReport = function(result){
        var el = YAHOO.util.Dom.get("mainpanel");
        el.innerHTML = result.report + "<div id=\"paginator\"></div>";

        executeDisplayReportCallbacks();
    }

    var executeDisplayReportCallbacks = function(){
        for(var i=0; i < displayReportCallbacks.length; i++){
            displayReportCallbacks[i]();
        }
    }

    var registerDisplayReportCallbacks = function(callback){
        displayReportCallbacks.push(callback); // check to make sure this is a function before adding to array
    }

    var messageResponseSuccess = function(o){
        try{
            var result = YAHOO.lang.JSON.parse(o.responseText);
            if(result.feedback == SUCCESS){
                var el = YAHOO.util.Dom.get("message_" + result.id);
                YAHOO.util.Dom.addClass(el, "hideDisplay");
            }else{
                alert("error: " + result.feedback);
            }
        }catch(e){
            alert(e.message);
        }
    }

    var messageResponseFailure = function(o){
        alert("failure");
    }

    var bindEvents = function(){
        YAHOO.util.Event.addListener(btnAddDomain, "click", openAddDomainPanel);
        YAHOO.util.Event.addListener(btnAdd, "click", addDomain);
        YAHOO.util.Event.addListener(btnViewReport, "click", viewReport);
        YAHOO.util.Event.addListener(btnCancel, "click", function(){
                addDomainPanel.hide();
        });
        
    }

    var addSelectOptions = function(results){
        for(var i=0; i < results.feedback.length; i++){
            selDomains.options[i]=new Option(results.feedback[i].domain_name, results.feedback[i].id, false, false)
        }
    }

    var clearSelectOptions = function(){
            selDomains.length = 0;
    }

    var responseSuccess = function(o){
        try{
            var result = YAHOO.lang.JSON.parse(o.responseText);

                // check for valid feedback
            if(result.feedback == INVALID_DATA){
                alert("Invalid Entry. Please try again.");
            }else{
                clearSelectOptions();
                addSelectOptions(result);
                addDomainPanel.hide();
                alert("You have successfully added a domain.");
            }
        }catch (e) {
            alert("failure");
        }
    }

    var responseFailure = function(o){
        alert("failure");
    }

    var callback = {
        success: responseSuccess,
        failure: responseFailure
    }

    var addDomain = function(){
        var txtDomain = YAHOO.util.Dom.get("txtAddDomain");
        var postQueryString = "domain=" + YAHOO.lang.trim(txtDomain.value);
        var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postQueryString);
    }

    var openAddDomainPanel = function(){
        addDomainPanel.show();
    }

    return{
        init : init,
        registerDisplayReportCallbacks:registerDisplayReportCallbacks
    }
	
}();
SM.YUILoader.registerCallback(SM.ControlPanel.init);